﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExtEasy.Base;

namespace ExtEasy.Controls
{
    public class SettingEditor : ExtControl
    {
        public SettingEditor(string id)
            : base(id)
        {
            NewKey = string.Empty;
            NewValue = string.Empty;
            Caption = "Справочник значений";
        }

        public Dictionary<string,string> Dictionary { get; set; }
        public string NewKey { get; set; }
        public string NewValue { get; set; }
        public string Caption { get; set; }

        public string GetDictValue(string key)
        {
            return Dictionary[key];
        }

        public override ExtAjaxRes ProcessOperation(System.Collections.Specialized.NameValueCollection c)
        {
           // foreach(string val in Dictionary.Keys

            bool needrefresh = false;
            string script = string.Empty;
            try
            {               
                switch (c["op"])
                {
                    case "refresh": needrefresh = true; break;

                    case "reload": SettingReload(null); needrefresh = true; break;

                    case "setnewkey":
                        NewKey = c["val"].Trim();
                        if (Dictionary.Keys.Contains(NewKey)) throw new Exception("Запись с ключом "+ NewKey+" уже существует!");
                        script += "TryAddNewSetting('"+this.CtrlKey+"');";
                        break;

                    case "setnewvalue":
                        NewValue = c["val"].Trim();
                        script += "TryAddNewSetting('" + this.CtrlKey + "');";
                        break;

                    case "setvalue":
                        string key = c["key"].Trim();
                        string value = c["val"].Trim();                        
                        SettingChanged(new SettingsEventArgs { Key = key, Value = value });
                        Dictionary[key] = value;
                        needrefresh = true;
                        NewKey = string.Empty;
                        NewValue = string.Empty;
                        break;

                    case "deletevalue":
                        SettingDelete(new SettingsEventArgs { Key = c["key"], Value = Dictionary[c["key"]] });
                        Dictionary.Remove(c["key"]);
                        needrefresh = true;
                        break;

                    default: throw new Exception("Не найдена операция " + c["op"]);
                }
            }
            catch (Exception ex)
            {
                script += "alert('" + ex.Message/* + " " + ex.StackTrace*/+ "');";
            }

            if (needrefresh)
            {
                script += "BindSettingEvents();BindDeleteSettingEvents();";
            }

            Render();
            return needrefresh ? new ExtAjaxRes { ID = ID, HTML = HTML, BackScript = script } : new ExtAjaxRes { BackScript = script };
        }

        public delegate void SettingEventHandler(object sender, SettingsEventArgs arg);
        public event SettingEventHandler OnSettingChanged;
        public event SettingEventHandler OnSettingDelete;
        public event SettingEventHandler OnSettingReload;

        protected void SettingChanged(SettingsEventArgs arg)
        {
            if (OnSettingChanged != null) OnSettingChanged(this, arg);
        }

        protected void SettingDelete(SettingsEventArgs arg)
        {
            if (OnSettingDelete != null) OnSettingDelete(this, arg);
        }

        protected void SettingReload(SettingsEventArgs arg)
        {
            if (OnSettingReload != null) OnSettingReload(this, arg);
        }
    }

    public class SettingsEventArgs : EventArgs
    {
        public string Key { get; set; }
        public string Value { get; set; }
    }
}
